package engine.newUi
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.logic.FunctionGuideLogic;
	import engine.logic.SpanSceneMoveLogic;
	import engine.ui.UIManage;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.window.NxWindowDragType;
	import newx.com.control.window.NxWindowEvent;
	
	public class FunctionGuideUI extends BaseUI
	{
		private static var self:FunctionGuideUI;
		private var model:ModelLocator;
		
		public var picMC:MovieClip;
		public var guidePic:NxClip;
		public var guideBtn:NxButton;
		public var guideMC:MovieClip;
		private var hasBefore:Boolean	=	false;
		private var isFunctionGuide:Boolean	=	true;
		
		public function FunctionGuideUI(popupUI:Boolean=true)
		{
			super(popupUI);
			init();
		}
		
		public static function getInstance():FunctionGuideUI
		{
			if(self == null)
			{
				self = new FunctionGuideUI();
			}
			return self;
		}
		
		private function init():void
		{
			model	=	ModelLocator.getInstance(); 
		}
		
		override protected function BeforeShow():void
		{
			guideBtn.addEventListener(MouseEvent.CLICK,showGuide);
			windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			windowBG.dragSpaceType	=	NxWindowDragType.Window_NONE;
			guideMC.play();
//			SpanSceneMoveLogic.getInstance().stopGuide();
			GameData.Role.killMove();
			var guideObj:Object	=	StaticIni.getIniObj("GuideLevel_" + FunctionGuideLogic.getInstance().NowLevel );//FunctionGuideLogic.getInstance().NowLevel
			if(isFunctionGuide)
			{
				if(guideObj["picBefore"] && guideObj["picBefore"] != "")
				{
					hasBefore	=	true;
					showPic(ResPath.JPG + guideObj["picBefore"]);
				}
				else
				{
					if(guideObj["picSource"] != "")
					{
						showPic(ResPath.JPG + guideObj["picSource"]);
					}
					else
					{
						showGuide();
					}
				}
			}
		}
		
		public function setOtherGuide(picUrl:String):void
		{
			var url:String	=	StaticIni.getIniVar(picUrl,"picSource");
			isFunctionGuide	=	false;
			guidePic.source		=	ResPath.JPG + url;
			UIManage.getInstance().setCustUIMask(StageInfo.uiSpace,0.5,true,false);
		}
		
		private function showPic(picUrl:String):void
		{
			guidePic.source		=	picUrl;
			if(!hasBefore) UIManage.getInstance().setCustUIMask(StageInfo.uiSpace,0.5,true,false);
		}
		
		override protected function AfterHide():void
		{
			guideBtn.removeEventListener(MouseEvent.CLICK,showGuide);
			windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			guideMC.stop();
			guidePic.source	=	"";
		}
		
		private function onClose(e:NxWindowEvent):void
		{
			showGuide();
		}
		
		private function showGuide(e:MouseEvent = null):void
		{
			if(isFunctionGuide)
			{
				var guideObj:Object	=	StaticIni.getIniObj("GuideLevel_" + FunctionGuideLogic.getInstance().NowLevel );
				if(hasBefore)
				{
					if(guideObj["picSource"] != "")
					{
						showPic(ResPath.JPG + guideObj["picSource"]);
					}
					else
					{
						showGuide();
					}
					hasBefore = false;
				}
				else
				{
					FunctionGuideLogic.getInstance().showGuide(model.scene.Role.getPlayerLevel());
					UIManage.getInstance().cancelCustUIMask(true);
					hideCur();
					if(guideObj.hasOwnProperty("activityIconId"))
					{
						ActivitesGroupLogic.getInstance().addNewIconEffect(guideObj["activityIconId"]);
					}
				}
			}
			else
			{
				UIManage.getInstance().cancelCustUIMask(true);
				hideCur();
				isFunctionGuide = true;
			}
		}
	}
}